package com.proj.hot.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.utils.DebugUtil;
import com.proj.hot.utils.ImageLoaderConfigFactory;
import com.proj.hot.widget.CircleImageView;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 用户信息页
 */
public class UserInfoActivity extends BaseActivity {


    private CircleImageView user_logo;
    private TextView userInfoName;//名字
    private TextView userInfoPhone;//电话
    private TextView userInfoAddress;//地址

    private TextView grxx;

    private Button userInfoUpdate; //修改信息按钮
    private Button userInfoCancel; //返回上一页

    private String logo = "";
    private String name = "";
    private String phone = "";
    private String address = "";
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                DebugUtil.i("UserInfoActivity", "执行了hand,msg.what==0方法");
                setView(logo, name, phone, address);
                GlobalParams.user_name = name;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo);
        initView();
        setOnListener();

    }

    private void initView() {
        user_logo = (CircleImageView) this.findViewById(R.id.user_logo);
        userInfoName = (TextView) this.findViewById(R.id.userinfo_name);
        userInfoPhone = (TextView) this.findViewById(R.id.userinfo_phone);
        userInfoAddress = (TextView) this.findViewById(R.id.userinfo_address);
        userInfoUpdate = (Button) this.findViewById(R.id.userinfo_btn_update);
        userInfoCancel = (Button) this.findViewById(R.id.userinfo_btn_cancel);
        grxx = (TextView)this.findViewById(R.id.grxx);
        if (GlobalParams.screenHeight == 1080){
//				LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) holder.name
//						.getLayoutParams();
//				lp.setMargins(0, 0, 0, 0);
//				lp.height = 40;
//				holder.name.setLayoutParams(lp);
            grxx.setTextSize(25);

        }
        grxx.setText("<  "+"个人信息");

    }

    private void setView(String logo, String name, String phone, String addr) {
        ImageLoader.getInstance().displayImage(logo,
                user_logo, ImageLoaderConfigFactory.getOptionsWithNopic());
        userInfoName.setText(name);
        userInfoPhone.setText(phone);
        userInfoAddress.setText(addr);
    }

    private void setOnListener() {
        userInfoUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(UserInfoActivity.this.getApplicationContext(), UserInfoUpdateActivity.class);
                intent.putExtra("name",name);
                intent.putExtra("phone",phone);
                intent.putExtra("address",address);
                startActivity(intent);

            }
        });
        userInfoCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserInfoActivity.this.finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserData();
    }

    private void getUserData() {

        try {
            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("accountid", GlobalParams.user_id);


            final RequestParams params = new RequestParams(GlobalParams.reqHost + "account/getAccount");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        DebugUtil.i("UserInfoActivity", "用户信息返回数据=" + s);
                        DebugUtil.i("UserInfoActivity", "用户名称=" + GlobalParams.user_name);
                        JSONObject jData=new JSONObject(s);
                        JSONObject respbody=jData.getJSONObject("respbody").getJSONObject("data");
                        logo = respbody.getString("logo");
                        name = respbody.getString("name");
                        phone = respbody.getString("phone");
                        address = respbody.getString("address");
                        DebugUtil.i("UserInfoActivity", "解析出来的的数据：logo=" + logo + ",name=" + name + ",phone=" + phone + ",address=" + address);
                        handler.sendEmptyMessage(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
            DebugUtil.i("orderRecord", "获取用户信息异常！");
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(0);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
    }
}
